
package tests.laurens.cajo;

import gnu.cajo.invoke.Remote;
import gnu.cajo.utils.CodebaseServer;
import gnu.cajo.utils.ItemServer;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Test {

	public static class Proxy extends Panel implements ActionListener {
		private Object selfRef, item;
		Proxy() {
			super(new BorderLayout());
			Label label = new Label("Test Proxy", Label.CENTER);
			add(label, BorderLayout.CENTER);
			Button button = new Button("fun button!");
			button.addActionListener(this);
			add(button, BorderLayout.SOUTH);
		}
		public void setItem(Object item) {
			this.item = item;
		}
		public Component init(Object ref) {
			selfRef = ref;
			return this;
		}
		public void actionPerformed(ActionEvent e) {
			try {
				Remote.invoke(item, "pressed", null);
			} catch(Exception x) {
				x.printStackTrace();
			} // network failure
		}
		public Dimension getPreferredSize() {
			return new Dimension(300, 300);
		}
	}

	private Proxy proxy = new Proxy();
	public Component getProxy() {
		return proxy;
	}
	public void pressed() {
		System.out.println("I heard that!");
	}
	public static void main(String args[]) throws Exception {
		new CodebaseServer(null, 0);
		Test t = new Test();
		ItemServer.bind(t, "main", t.proxy);
		System.out.print("Server running on interface ");
		System.out.println(Remote.getDefaultServerHost());
		System.out.print("Using TCP port ");
		System.out.println(Remote.getDefaultServerPort());
		System.out.print("Codebase server running: ");
		System.out.println(System.getProperty("java.rmi.server.codebase"));
	}
}
